﻿using System;
using System.Text;

namespace Gonte
{
    public static class StringUtils
    {
        #region Inner Structures

        public struct StringReplacement
        {
            public string OldString;
            public string NewString;
        } 

        #endregion

        #region Static Methods

        /// <summary>
        /// Converts a string to camel case
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToCamelCase(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }

            return Char.ToLower(str[0]) + str.Substring(1);
        }

        /// <summary>
        /// Converts a string to pascal case
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToPascalCase(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }

            return Char.ToUpper(str[0]) + str.Substring(1);
        }

        /// <summary>
        /// Replaces part of a string with the array of replacements
        /// </summary>
        /// <param name="str"></param>
        /// <param name="replacements"></param>
        /// <returns></returns>
        public static string Replace(string str, StringReplacement[] replacements)
        {
            if (string.IsNullOrEmpty(str)
                || replacements == null)
            {
                return str;
            }

            StringBuilder stringBuilder = new StringBuilder(str);
            foreach (StringReplacement replacement in replacements)
            {
                stringBuilder.Replace(replacement.OldString, replacement.NewString);
            }

            return stringBuilder.ToString();
        }

        public static string MakeSingular(string objectName)
        {
            if (!objectName.EndsWith("sis"))
            {
                if (objectName.EndsWith("ies"))
                {
                    objectName = objectName.Remove(objectName.Length - 3, 3) + "y";
                }
                else if (objectName.EndsWith("ses")
                        || objectName.EndsWith("xes"))
                {
                    objectName = objectName.Remove(objectName.Length - 2, 2);
                }
                else if (objectName.EndsWith("s") && !objectName.EndsWith("ss") && !objectName.EndsWith("us"))
                {
                    objectName = objectName.Remove(objectName.Length - 1, 1);
                }
            }

            return objectName;
        }

        public static string MakePlural(string objectName)
        {
            if (!objectName.EndsWith("sis"))
            {
                // First make singular to avoid double plurals
                objectName = MakeSingular(objectName);

                if (objectName.EndsWith("y"))
                {
                    if (((string)"aeiou").IndexOf(objectName.Substring(objectName.Length - 2, 1)) > -1)
                        objectName += "s";
                    else
                        objectName = objectName.Remove(objectName.Length - 1, 1) + "ies";
                }
                else if (objectName.EndsWith("s"))
                {
                    objectName = objectName + "es";
                }
                else
                {
                    objectName = objectName + "s";
                }
            }

            return objectName;
        }

        #endregion
    }
}
